目标:在内核树之外构建一个 ebpf 程序,使用独立libbpf
和clang
问题:我遵循了这些步骤
1) 构建libbpf
git clone https://github.com/libbpf/libbpf.git
cd src
make
bpf_helpers.h
2)从我的内核 src复制/tools/testing/selftests/bpf/bpf_helpers.h
3)编译
clang -S -I. -O2 -Wall -Werror -emit-llvm -c mine_kern.c -o - | llc -march=bpf -filetype=obj -o mine_kern.o
但结果我有很多与bpf_helpers.h
内容有关的错误。例如
In file included from mine_kern.c:7:
./bpf_helpers.h:75:11: error: use of undeclared identifier
'BPF_FUNC_sock_ops_cb_flags_set'
(void *) BPF_FUNC_sock_ops_cb_flags_set;
^
mine_kern.c
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/ip.h>
#include <linux/in.h>
#include "bpf_helpers.h"
SEC("socket")
int bpfrog1(struct __sk_buff *skb)
{
int proto = load_byte(skb, ETH_HLEN + offsetof(struct iphdr, protocol));
if (proto == IPPROTO_ICMP && skb->pkt_type == PACKET_OUTGOING)
return -1;
else
return 0;
}
char _license[] SEC("license") = "GPL";
问题:
项目树是:
libbpf
|src
|libbpf.a
|libbpf.so
|...
libbpfebpf
|bpf_headers.h
|mine_kern.c
|mine_user.c
我该如何解决这个错误?修复后足以编译_user.c
和链接?gcc
-L../libbpf/src
libbpf.so