Skip to content

Commit e7e6c77

Browse files
DanielTimLeeAlexei Starovoitov
authored andcommitted
samples/bpf: convert to vmlinux.h with tracing programs
This commit replaces separate headers with a single vmlinux.h to tracing programs. Thanks to that, we no longer need to define the argument structure for tracing programs directly. For example, argument for the sched_switch tracpepoint (sched_switch_args) can be replaced with the vmlinux.h provided trace_event_raw_sched_switch. Additional defines have been added to the BPF program either directly or through the inclusion of net_shared.h. Defined values are PERF_MAX_STACK_DEPTH, IFNAMSIZ constants and __stringify() macro. This change enables the BPF program to access internal structures with BTF generated "vmlinux.h" header. Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com> Link: https://lore.kernel.org/r/20230818090119.477441-3-danieltimlee@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 34f6e38 commit e7e6c77

10 files changed

Lines changed: 25 additions & 62 deletions

samples/bpf/net_shared.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#define TC_ACT_OK 0
1818
#define TC_ACT_SHOT 2
1919

20+
#define IFNAMSIZ 16
21+
2022
#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
2123
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2224
#define bpf_ntohs(x) __builtin_bswap16(x)

samples/bpf/offwaketime_kern.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
* modify it under the terms of version 2 of the GNU General Public
55
* License as published by the Free Software Foundation.
66
*/
7-
#include <uapi/linux/bpf.h>
8-
#include <uapi/linux/ptrace.h>
9-
#include <uapi/linux/perf_event.h>
7+
#include "vmlinux.h"
108
#include <linux/version.h>
11-
#include <linux/sched.h>
129
#include <bpf/bpf_helpers.h>
1310
#include <bpf/bpf_tracing.h>
1411

12+
#ifndef PERF_MAX_STACK_DEPTH
13+
#define PERF_MAX_STACK_DEPTH 127
14+
#endif
15+
1516
#define _(P) \
1617
({ \
1718
typeof(P) val; \
@@ -111,18 +112,8 @@ static inline int update_counts(void *ctx, u32 pid, u64 delta)
111112

112113
#if 1
113114
/* taken from /sys/kernel/tracing/events/sched/sched_switch/format */
114-
struct sched_switch_args {
115-
unsigned long long pad;
116-
char prev_comm[TASK_COMM_LEN];
117-
int prev_pid;
118-
int prev_prio;
119-
long long prev_state;
120-
char next_comm[TASK_COMM_LEN];
121-
int next_pid;
122-
int next_prio;
123-
};
124115
SEC("tracepoint/sched/sched_switch")
125-
int oncpu(struct sched_switch_args *ctx)
116+
int oncpu(struct trace_event_raw_sched_switch *ctx)
126117
{
127118
/* record previous thread sleep time */
128119
u32 pid = ctx->prev_pid;

samples/bpf/spintest_kern.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
* modify it under the terms of version 2 of the GNU General Public
55
* License as published by the Free Software Foundation.
66
*/
7-
#include <linux/skbuff.h>
8-
#include <linux/netdevice.h>
7+
#include "vmlinux.h"
98
#include <linux/version.h>
10-
#include <uapi/linux/bpf.h>
11-
#include <uapi/linux/perf_event.h>
129
#include <bpf/bpf_helpers.h>
1310
#include <bpf/bpf_tracing.h>
1411

12+
#ifndef PERF_MAX_STACK_DEPTH
13+
#define PERF_MAX_STACK_DEPTH 127
14+
#endif
15+
1516
struct {
1617
__uint(type, BPF_MAP_TYPE_HASH);
1718
__type(key, long);
@@ -60,6 +61,7 @@ SEC("kprobe/_raw_spin_lock_irq")PROG(p11)
6061
SEC("kprobe/_raw_spin_trylock")PROG(p12)
6162
SEC("kprobe/_raw_spin_lock")PROG(p13)
6263
SEC("kprobe/_raw_spin_lock_bh")PROG(p14)
64+
6365
/* and to inner bpf helpers */
6466
SEC("kprobe/htab_map_update_elem")PROG(p15)
6567
SEC("kprobe/__htab_percpu_map_update_elem")PROG(p16)

samples/bpf/test_overhead_tp.bpf.c

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,15 @@
88
#include <bpf/bpf_helpers.h>
99

1010
/* from /sys/kernel/tracing/events/task/task_rename/format */
11-
struct task_rename {
12-
__u64 pad;
13-
__u32 pid;
14-
char oldcomm[TASK_COMM_LEN];
15-
char newcomm[TASK_COMM_LEN];
16-
__u16 oom_score_adj;
17-
};
1811
SEC("tracepoint/task/task_rename")
19-
int prog(struct task_rename *ctx)
12+
int prog(struct trace_event_raw_task_rename *ctx)
2013
{
2114
return 0;
2215
}
2316

2417
/* from /sys/kernel/tracing/events/fib/fib_table_lookup/format */
25-
struct fib_table_lookup {
26-
__u64 pad;
27-
__u32 tb_id;
28-
int err;
29-
int oif;
30-
int iif;
31-
__u8 proto;
32-
__u8 tos;
33-
__u8 scope;
34-
__u8 flags;
35-
__u8 src[4];
36-
__u8 dst[4];
37-
__u8 gw4[4];
38-
__u8 gw6[16];
39-
__u16 sport;
40-
__u16 dport;
41-
char name[16];
42-
};
4318
SEC("tracepoint/fib/fib_table_lookup")
44-
int prog2(struct fib_table_lookup *ctx)
19+
int prog2(struct trace_event_raw_fib_table_lookup *ctx)
4520
{
4621
return 0;
4722
}

samples/bpf/tracex1_kern.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
* modify it under the terms of version 2 of the GNU General Public
55
* License as published by the Free Software Foundation.
66
*/
7-
#include <linux/skbuff.h>
8-
#include <linux/netdevice.h>
9-
#include <uapi/linux/bpf.h>
7+
#include "vmlinux.h"
8+
#include "net_shared.h"
109
#include <linux/version.h>
1110
#include <bpf/bpf_helpers.h>
1211
#include <bpf/bpf_tracing.h>

samples/bpf/tracex3_kern.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
* modify it under the terms of version 2 of the GNU General Public
55
* License as published by the Free Software Foundation.
66
*/
7-
#include <linux/skbuff.h>
8-
#include <linux/netdevice.h>
7+
#include "vmlinux.h"
98
#include <linux/version.h>
10-
#include <uapi/linux/bpf.h>
119
#include <bpf/bpf_helpers.h>
1210
#include <bpf/bpf_tracing.h>
1311

samples/bpf/tracex4_kern.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
* modify it under the terms of version 2 of the GNU General Public
55
* License as published by the Free Software Foundation.
66
*/
7-
#include <linux/ptrace.h>
7+
#include "vmlinux.h"
88
#include <linux/version.h>
9-
#include <uapi/linux/bpf.h>
109
#include <bpf/bpf_helpers.h>
1110
#include <bpf/bpf_tracing.h>
1211

samples/bpf/tracex5_kern.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
* modify it under the terms of version 2 of the GNU General Public
55
* License as published by the Free Software Foundation.
66
*/
7-
#include <linux/ptrace.h>
7+
#include "vmlinux.h"
8+
#include "syscall_nrs.h"
89
#include <linux/version.h>
9-
#include <uapi/linux/bpf.h>
10-
#include <uapi/linux/seccomp.h>
1110
#include <uapi/linux/unistd.h>
12-
#include "syscall_nrs.h"
1311
#include <bpf/bpf_helpers.h>
1412
#include <bpf/bpf_tracing.h>
1513

14+
#define __stringify(x) #x
1615
#define PROG(F) SEC("kprobe/"__stringify(F)) int bpf_func_##F
1716

1817
struct {

samples/bpf/tracex6_kern.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#include <linux/ptrace.h>
1+
#include "vmlinux.h"
22
#include <linux/version.h>
3-
#include <uapi/linux/bpf.h>
43
#include <bpf/bpf_helpers.h>
54

65
struct {

samples/bpf/tracex7_kern.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#include <uapi/linux/ptrace.h>
2-
#include <uapi/linux/bpf.h>
1+
#include "vmlinux.h"
32
#include <linux/version.h>
43
#include <bpf/bpf_helpers.h>
54

0 commit comments

Comments
 (0)