这是我的简单的ebpf程序
#include <linux/bpf.h>
#include <linux/version.h>
#include <linux/ip.h>
#include <linux/if_ether.h>
#include <bpf_helpers.h>
#include <bpf_endian.h>// iproute specifically looks for this ELF section
//#include <net/sock.h>
#include <linux/bpf.h>
#include <linux/version.h>
#include <bpf_helpers.h>
#define bpf_printk(fmt, ...) \
({ \
char ____fmt[] = fmt; \
bpf_trace_printk(____fmt, sizeof(____fmt), \
##__VA_ARGS__); \
})
SEC("kprobe/tcp_connect")
int connect(struct pt_regs *ctx)
{
bpf_printk("connect called -- Hello from [fawad] \n");
return 0;
}
char _license[] SEC("license") = "GPL";使用以下命令编译上述程序
clang -O2 -Wall -target bpf -c -o xdp.o -I /build/root/usr/include/bpf -I /usr/src/linux-headers-5.11.0-41/arch/alpha/include/ -I /usr/src/linux-headers-5.11.0-41/arch/alpha/include/ xdp.c这是一个加载程序
#include <stdlib.h>
#include <unistd.h>
#include <sys/resource.h>
#include <bpf/libbpf.h>
#include <bpf_load.h>
int main() { // raise the rlimit or see
// failed to create a map: 1 Operation not permitted
// when load_bpf_file is run
if (load_bpf_file("xdp.o"))
{
printf("%s\n", bpf_log_buf);
return 1;
}
while(1){
sleep(1);
}
} return 0;
}像这样编译我的ebpf加载程序
clang -O2 -Wall -target bpf -c -o user.o -I /build/root/usr/include/bpf -I /home/ubuntu/Desktop/linux/test/linux-5.14.1/samples/bpf/ -I /usr/src/linux-headers-5.11.0-41/arch/alpha/include/ -I /usr/src/linux-headers-5.11.0-41/arch/alpha/include/ user.c 当我像#./user.o这样运行加载程序时
它会产生错误
bash: ./user.o: Permission denied与sudo一起运行甚至无法识别该文件。
root@this:/home/ubuntu/Desktop/ebpf# sudo ./user.o
sudo: ./user.o: command not found
root@this:/home/ubuntu/Desktop/ebpf# 发布于 2021-12-16 09:53:03
只有您的eBPF程序需要编译为eBPF,并具有-t bpf目标。只需定期使用clang或gcc编译加载程序即可。
$ clang -Wall -o user user.c然后,您应该能够在此之后运行您的可执行文件(尽管我还没有检查您的程序是否按预期工作):
$ sudo ./user(注意:您可能仍然需要在编译时传递一些包含路径、-I /home/ubuntu/Desktop/linux/test/linux-5.14.1/samples/bpf/ for <bpf_load.h>以及可能传递到<bpf/libbpf.h>的路径)
https://stackoverflow.com/questions/70376637
复制相似问题