-2

Why I am getting this error when I want to load the ebpf program into kernel??

ebpf_prog.c:

#include <bpf/bpf_helpers.h>
#include <bpf/libbpf.h>

int main(int argc, char **argv) {
    struct bpf_object *obj;
    int map_fd, prog_fd;
    int i, sock;
    FILE *f;
    
if (bpf_prog_load("ebpf_prog.o", BPF_PROG_TYPE_SOCKET_FILTER, &obj, &prog_fd)){
        printf("The kernel didn't load the BPF program\n");
        return -1;
    }

  return 0;
}

load_prog.c:

#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/libbpf.h>

int main(int argc, char **argv) {
    struct bpf_object *obj;
    int map_fd, prog_fd;
    int i, sock;
    FILE *f;


    if (bpf_prog_load("ebpf_prog.o", BPF_PROG_TYPE_SOCKET_FILTER, &obj, &prog_fd)){
        printf("The kernel didn't load the BPF program\n");
        return -1;
    }

  return 0;
}

Error:

$ gcc ebpf_prog.c -c -o ebpf_prog.o
$ gcc load_prog.c -o load_prog -lbpf
$ ./load_prog
libbpf: elf: sock_example.o is not a valid eBPF object file
The kernel didn't load the BPF program

what's wrong with my code??

2 Answers 2

2

Your load_prog.c refers to sock_example.o as the object file containing the BPF program, but that object file is actually called ebpf_prog.o.

1
  • Hi pchaingo. No that's OK. it is because of typo mistake in my question and the Orginal file is correct. Commented May 13, 2021 at 11:46
2

Your ebpf_prog.c isn’t a valid eBPF program, but that might just be a copy-paste error since it’s nearly the same as your load_prog.c. (I’m assuming the ebpf_prog.o/sock_example.o mismatch is also a copy-paste error.)

The main error is that you’re building it as an ELF binary; eBPF programs need to be built as eBPF byte code:

clang -target bpf ...

Since you’re using libbpf, see the libbpf bootstrap tutorial.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .