I build a simple test platform with linux + busybox on Arm FVP. And I cross compile an simple ebpf program and run it on the platform, but received: bpf load prog failed: Invalid argument
. How to solve it?
I asked more complex question yesterday, but not solved yet. And thus I try load an ebpf program directly through syscall, rather than lib support, like libbpf or BCC. But I still failed to run it.
I already configure the kernel to enable bpf:
CONFIG_BPF=y
CONFIG_HAVE_EBPF_JIT=y
CONFIG_ARCH_WANT_DEFAULT_BPF_JIT=y
#
# BPF subsystem
#
CONFIG_BPF_SYSCALL=y
# CONFIG_BPF_JIT is not set
CONFIG_BPF_UNPRIV_DEFAULT_OFF=y
# CONFIG_BPF_PRELOAD is not set
# end of BPF subsystem
And then I statically cross compile a simple ebpf program, the main body post here:
int bpf(enum bpf_cmd cmd, union bpf_attr *attr, unsigned int size) {
return syscall(__NR_bpf, cmd, attr, size);
}
int bpf_prog_load(enum bpf_prog_type type, const struct bpf_insn* insns, int insn_cnt, const char* license) {
union bpf_attr attr = {
.prog_type = type,
.insns = ptr_to_u64(insns),
.insn_cnt = insn_cnt,
.license = ptr_to_u64(license),
.log_buf = ptr_to_u64(bpf_log_buf),
.log_size = LOG_BUF_SIZE,
.log_level = 2,
};
return bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
}
struct bpf_insn bpf_prog[] = {
BPF_MOV64_IMM(BPF_REG_1, 10), // r1 = 10
BPF_MOV64_IMM(BPF_REG_0, 0), // r0 = 0
BPF_EXIT_INSN(), // exit
};
int main(){
int prog_fd, perf_fd, link_fd;
// load
prog_fd = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, bpf_prog, sizeof(bpf_prog)/sizeof(bpf_prog[0]), "GPL");
if (prog_fd < 0) {
perror("bpf load prog failed");
exit(-1);
}
}
And I compile it:
$ aarch64-linux-gnu-gcc loader.c -static -o loader
$ file loader
loader: ELF 64-bit LSB executable, ARM aarch64, version 1 (GNU/Linux), statically linked, BuildID[sha1]=f4d82df351dd47ce9373605cf1ab9d6f47180965, for GNU/Linux 3.7.0, not stripped
Then I run the binary in the arm64 test platform and meet with the errors.
How to solve it? Maybe I lack some environment or incorrect configs?