Loading...
Loading...
Compare original and translation side by side
Goal?
├── One-liner kernel tracing / scripting → bpftrace
├── Production eBPF program with userspace → libbpf (C) or aya (Rust)
├── Inspect loaded programs and maps → bpftool
└── High-performance packet processing → XDP + libbpfGoal?
├── One-liner kernel tracing / scripting → bpftrace
├── Production eBPF program with userspace → libbpf (C) or aya (Rust)
├── Inspect loaded programs and maps → bpftool
└── High-performance packet processing → XDP + libbpfundefinedundefinedundefinedundefined// counter.bpf.c — kernel-side
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, u32);
__type(value, u64);
__uint(max_entries, 1024);
} call_count SEC(".maps");
SEC("tracepoint/syscalls/sys_enter_read")
int trace_read(struct trace_event_raw_sys_enter *ctx)
{
u32 pid = bpf_get_current_pid_tgid() >> 32;
u64 *cnt = bpf_map_lookup_elem(&call_count, &pid);
if (cnt)
(*cnt)++;
else {
u64 one = 1;
bpf_map_update_elem(&call_count, &pid, &one, BPF_ANY);
}
return 0;
}
char LICENSE[] SEC("license") = "GPL";// counter.c — userspace loader
#include "counter.skel.h"
int main(void) {
struct counter_bpf *skel = counter_bpf__open_and_load();
counter_bpf__attach(skel);
// read map, print results
counter_bpf__destroy(skel);
}undefined// counter.bpf.c — kernel-side
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, u32);
__type(value, u64);
__uint(max_entries, 1024);
} call_count SEC(".maps");
SEC("tracepoint/syscalls/sys_enter_read")
int trace_read(struct trace_event_raw_sys_enter *ctx)
{
u32 pid = bpf_get_current_pid_tgid() >> 32;
u64 *cnt = bpf_map_lookup_elem(&call_count, &pid);
if (cnt)
(*cnt)++;
else {
u64 one = 1;
bpf_map_update_elem(&call_count, &pid, &one, BPF_ANY);
}
return 0;
}
char LICENSE[] SEC("license") = "GPL";// counter.c — userspace loader
#include "counter.skel.h"
int main(void) {
struct counter_bpf *skel = counter_bpf__open_and_load();
counter_bpf__attach(skel);
// read map, print results
counter_bpf__destroy(skel);
}undefinedundefinedundefined| Map type | Key→Value | Use case |
|---|---|---|
| arbitrary→arbitrary | Per-PID counters, state |
| u32→fixed | Config, metrics indexed by CPU |
| key→per-CPU val | High-frequency counters without locks |
| — | Efficient kernel→userspace events |
| — | Legacy perf event output |
| key→val | Connection tracking, limited size |
| u32→prog | Tail calls, program chaining |
| — | AF_XDP socket redirection |
BPF_MAP_TYPE_RINGBUFPERF_EVENT_ARRAY| 映射表类型 | 键→值 | 适用场景 |
|---|---|---|
| 任意类型→任意类型 | 按PID统计的计数器、状态存储 |
| u32→固定类型 | 配置存储、按CPU索引的指标 |
| 键→每CPU值 | 无锁的高频计数器 |
| — | 高效的内核→用户态事件传输 |
| — | 传统perf事件输出 |
| 键→值 | 连接追踪、有限容量存储 |
| u32→程序 | 尾调用、程序链式调用 |
| — | AF_XDP套接字重定向 |
BPF_MAP_TYPE_RINGBUFPERF_EVENT_ARRAY| Error message | Root cause | Fix |
|---|---|---|
| Dereferencing unbounded pointer | Check pointer with null test before use |
| Return without setting R0 | Ensure all paths set a return value |
| Branch target beyond program end | Restructure conditionals |
| Backward jump (loop) | Use |
| Dead code after return | Remove dead branches |
| Stack read of uninitialised bytes | Zero-init structs: |
| Pointer arithmetic off alignment | Align reads to |
undefined| 错误信息 | 根本原因 | 修复方案 |
|---|---|---|
| 解引用未受限的指针 | 使用前先对指针进行空值检查 |
| 返回时未设置R0寄存器 | 确保所有代码路径都设置了返回值 |
| 分支目标超出程序范围 | 重构条件语句 |
| 向后跳转(循环) | 使用 |
| 返回语句后存在死代码 | 删除无用分支 |
| 读取栈上未初始化的字节 | 零初始化结构体: |
| 指针算术运算导致对齐错误 | 将读取操作对齐到 |
undefinedundefinedundefined// xdp_drop_icmp.bpf.c
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
SEC("xdp")
int xdp_filter(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct ethhdr *eth = data;
if ((void *)(eth + 1) > data_end)
return XDP_PASS;
if (bpf_ntohs(eth->h_proto) != ETH_P_IP)
return XDP_PASS;
struct iphdr *ip = (void *)(eth + 1);
if ((void *)(ip + 1) > data_end)
return XDP_PASS;
if (ip->protocol == IPPROTO_ICMP)
return XDP_DROP;
return XDP_PASS;
}
char LICENSE[] SEC("license") = "GPL";undefined// xdp_drop_icmp.bpf.c
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
SEC("xdp")
int xdp_filter(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct ethhdr *eth = data;
if ((void *)(eth + 1) > data_end)
return XDP_PASS;
if (bpf_ntohs(eth->h_proto) != ETH_P_IP)
return XDP_PASS;
struct iphdr *ip = (void *)(eth + 1);
if ((void *)(ip + 1) > data_end)
return XDP_PASS;
if (ip->protocol == IPPROTO_ICMP)
return XDP_DROP;
return XDP_PASS;
}
char LICENSE[] SEC("license") = "GPL";undefined
XDP return codes: `XDP_PASS`, `XDP_DROP`, `XDP_TX` (hairpin), `XDP_REDIRECT`.
XDP返回码:`XDP_PASS`、`XDP_DROP`、`XDP_TX`(回环)、`XDP_REDIRECT`。// Use BTF-based field access (CO-RE aware)
#include <vmlinux.h> // generated from running kernel's BTF
#include <bpf/bpf_core_read.h>
SEC("kprobe/tcp_connect")
int trace_connect(struct pt_regs *ctx)
{
struct sock *sk = (struct sock *)PT_REGS_PARM1(ctx);
u16 dport = BPF_CORE_READ(sk, __sk_common.skc_dport);
// BPF_CORE_READ relocates the field offset at load time
bpf_printk("connect to port %d\n", bpf_ntohs(dport));
return 0;
}undefined// Use BTF-based field access (CO-RE aware)
#include <vmlinux.h> // generated from running kernel's BTF
#include <bpf/bpf_core_read.h>
SEC("kprobe/tcp_connect")
int trace_connect(struct pt_regs *ctx)
{
struct sock *sk = (struct sock *)PT_REGS_PARM1(ctx);
u16 dport = BPF_CORE_READ(sk, __sk_common.skc_dport);
// BPF_CORE_READ relocates the field offset at load time
bpf_printk("connect to port %d\n", bpf_ntohs(dport));
return 0;
}undefined
For the full map types reference, see [references/ebpf-map-types.md](references/ebpf-map-types.md).
完整的映射表类型参考,请查看[references/ebpf-map-types.md](references/ebpf-map-types.md)。skills/observability/ebpf-rustskills/profilers/linux-perfskills/runtimes/binary-hardeningskills/low-level-programming/linux-kernel-modulesskills/observability/ebpf-rustskills/profilers/linux-perfskills/runtimes/binary-hardeningskills/low-level-programming/linux-kernel-modules