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

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: default avatarDaniel T. Lee <danieltimlee@gmail.com>
Link: https://lore.kernel.org/r/20230818090119.477441-3-danieltimlee@gmail.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 34f6e38f
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
#define TC_ACT_OK 0 #define TC_ACT_OK 0
#define TC_ACT_SHOT 2 #define TC_ACT_SHOT 2
#define IFNAMSIZ 16
#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \ #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define bpf_ntohs(x) __builtin_bswap16(x) #define bpf_ntohs(x) __builtin_bswap16(x)
......
...@@ -4,14 +4,15 @@ ...@@ -4,14 +4,15 @@
* modify it under the terms of version 2 of the GNU General Public * modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation. * License as published by the Free Software Foundation.
*/ */
#include <uapi/linux/bpf.h> #include "vmlinux.h"
#include <uapi/linux/ptrace.h>
#include <uapi/linux/perf_event.h>
#include <linux/version.h> #include <linux/version.h>
#include <linux/sched.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h> #include <bpf/bpf_tracing.h>
#ifndef PERF_MAX_STACK_DEPTH
#define PERF_MAX_STACK_DEPTH 127
#endif
#define _(P) \ #define _(P) \
({ \ ({ \
typeof(P) val; \ typeof(P) val; \
...@@ -111,18 +112,8 @@ static inline int update_counts(void *ctx, u32 pid, u64 delta) ...@@ -111,18 +112,8 @@ static inline int update_counts(void *ctx, u32 pid, u64 delta)
#if 1 #if 1
/* taken from /sys/kernel/tracing/events/sched/sched_switch/format */ /* taken from /sys/kernel/tracing/events/sched/sched_switch/format */
struct sched_switch_args {
unsigned long long pad;
char prev_comm[TASK_COMM_LEN];
int prev_pid;
int prev_prio;
long long prev_state;
char next_comm[TASK_COMM_LEN];
int next_pid;
int next_prio;
};
SEC("tracepoint/sched/sched_switch") SEC("tracepoint/sched/sched_switch")
int oncpu(struct sched_switch_args *ctx) int oncpu(struct trace_event_raw_sched_switch *ctx)
{ {
/* record previous thread sleep time */ /* record previous thread sleep time */
u32 pid = ctx->prev_pid; u32 pid = ctx->prev_pid;
......
...@@ -4,14 +4,15 @@ ...@@ -4,14 +4,15 @@
* modify it under the terms of version 2 of the GNU General Public * modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation. * License as published by the Free Software Foundation.
*/ */
#include <linux/skbuff.h> #include "vmlinux.h"
#include <linux/netdevice.h>
#include <linux/version.h> #include <linux/version.h>
#include <uapi/linux/bpf.h>
#include <uapi/linux/perf_event.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h> #include <bpf/bpf_tracing.h>
#ifndef PERF_MAX_STACK_DEPTH
#define PERF_MAX_STACK_DEPTH 127
#endif
struct { struct {
__uint(type, BPF_MAP_TYPE_HASH); __uint(type, BPF_MAP_TYPE_HASH);
__type(key, long); __type(key, long);
...@@ -60,6 +61,7 @@ SEC("kprobe/_raw_spin_lock_irq")PROG(p11) ...@@ -60,6 +61,7 @@ SEC("kprobe/_raw_spin_lock_irq")PROG(p11)
SEC("kprobe/_raw_spin_trylock")PROG(p12) SEC("kprobe/_raw_spin_trylock")PROG(p12)
SEC("kprobe/_raw_spin_lock")PROG(p13) SEC("kprobe/_raw_spin_lock")PROG(p13)
SEC("kprobe/_raw_spin_lock_bh")PROG(p14) SEC("kprobe/_raw_spin_lock_bh")PROG(p14)
/* and to inner bpf helpers */ /* and to inner bpf helpers */
SEC("kprobe/htab_map_update_elem")PROG(p15) SEC("kprobe/htab_map_update_elem")PROG(p15)
SEC("kprobe/__htab_percpu_map_update_elem")PROG(p16) SEC("kprobe/__htab_percpu_map_update_elem")PROG(p16)
......
...@@ -8,40 +8,15 @@ ...@@ -8,40 +8,15 @@
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
/* from /sys/kernel/tracing/events/task/task_rename/format */ /* from /sys/kernel/tracing/events/task/task_rename/format */
struct task_rename {
__u64 pad;
__u32 pid;
char oldcomm[TASK_COMM_LEN];
char newcomm[TASK_COMM_LEN];
__u16 oom_score_adj;
};
SEC("tracepoint/task/task_rename") SEC("tracepoint/task/task_rename")
int prog(struct task_rename *ctx) int prog(struct trace_event_raw_task_rename *ctx)
{ {
return 0; return 0;
} }
/* from /sys/kernel/tracing/events/fib/fib_table_lookup/format */ /* from /sys/kernel/tracing/events/fib/fib_table_lookup/format */
struct fib_table_lookup {
__u64 pad;
__u32 tb_id;
int err;
int oif;
int iif;
__u8 proto;
__u8 tos;
__u8 scope;
__u8 flags;
__u8 src[4];
__u8 dst[4];
__u8 gw4[4];
__u8 gw6[16];
__u16 sport;
__u16 dport;
char name[16];
};
SEC("tracepoint/fib/fib_table_lookup") SEC("tracepoint/fib/fib_table_lookup")
int prog2(struct fib_table_lookup *ctx) int prog2(struct trace_event_raw_fib_table_lookup *ctx)
{ {
return 0; return 0;
} }
......
...@@ -4,9 +4,8 @@ ...@@ -4,9 +4,8 @@
* modify it under the terms of version 2 of the GNU General Public * modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation. * License as published by the Free Software Foundation.
*/ */
#include <linux/skbuff.h> #include "vmlinux.h"
#include <linux/netdevice.h> #include "net_shared.h"
#include <uapi/linux/bpf.h>
#include <linux/version.h> #include <linux/version.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h> #include <bpf/bpf_tracing.h>
......
...@@ -4,10 +4,8 @@ ...@@ -4,10 +4,8 @@
* modify it under the terms of version 2 of the GNU General Public * modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation. * License as published by the Free Software Foundation.
*/ */
#include <linux/skbuff.h> #include "vmlinux.h"
#include <linux/netdevice.h>
#include <linux/version.h> #include <linux/version.h>
#include <uapi/linux/bpf.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h> #include <bpf/bpf_tracing.h>
......
...@@ -4,9 +4,8 @@ ...@@ -4,9 +4,8 @@
* modify it under the terms of version 2 of the GNU General Public * modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation. * License as published by the Free Software Foundation.
*/ */
#include <linux/ptrace.h> #include "vmlinux.h"
#include <linux/version.h> #include <linux/version.h>
#include <uapi/linux/bpf.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h> #include <bpf/bpf_tracing.h>
......
...@@ -4,15 +4,14 @@ ...@@ -4,15 +4,14 @@
* modify it under the terms of version 2 of the GNU General Public * modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation. * License as published by the Free Software Foundation.
*/ */
#include <linux/ptrace.h> #include "vmlinux.h"
#include "syscall_nrs.h"
#include <linux/version.h> #include <linux/version.h>
#include <uapi/linux/bpf.h>
#include <uapi/linux/seccomp.h>
#include <uapi/linux/unistd.h> #include <uapi/linux/unistd.h>
#include "syscall_nrs.h"
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h> #include <bpf/bpf_tracing.h>
#define __stringify(x) #x
#define PROG(F) SEC("kprobe/"__stringify(F)) int bpf_func_##F #define PROG(F) SEC("kprobe/"__stringify(F)) int bpf_func_##F
struct { struct {
......
#include <linux/ptrace.h> #include "vmlinux.h"
#include <linux/version.h> #include <linux/version.h>
#include <uapi/linux/bpf.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
struct { struct {
......
#include <uapi/linux/ptrace.h> #include "vmlinux.h"
#include <uapi/linux/bpf.h>
#include <linux/version.h> #include <linux/version.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
......
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