nav
nav

Reputation: 440

BPF program using XDP returns failed to load BPF skeleton (-22)

Firstly, I was not using the libbpf API directly, neither BCC. Instead, I was trying to use the API of the skeleton generated by bpftool.

Control code:

obj_gen = bpf_xdp_c__open();
if (!obj_gen)
  goto cleanup;

ifindex = if_nametoindex("eth0");
if(!ifindex)
{
  perror("if_nametoindex");
  return 1;
}

err = bpf_xdp_c__load(obj_gen);

BPF code:

// Simple XDP BPF program. Everything packet will be dropped. 
SEC("test")
int xdp_prog1(struct xdp_md *ctx){
  char drop_message[] = "XDP PACKET DROP\n";
  bpf_trace_printk(&drop_message, sizeof(drop_message));
  return XDP_DROP;
}

So, after running, the error below was shown:

  // libbpf: load bpf program failed: Invalid argument
  // libbpf: failed to load program 'test'
  // libbpf: failed to load object 'bpf_xdp_c'
  // libbpf: failed to load BPF skeleton 'bpf_xdp_c': -22

After debugging, I have noticed that the program type was not with the correct value. It was always returning 0. So, I had to define the following code before the load call:

 obj_gen->progs.xdp_prog1->type = BPF_PROG_TYPE_XDP;

Because the struct bpf_program was in the libbpf.c, I had to redefine in my header in in order to the compile find it. This workaround worked.

Q: Is there a better solution?

Upvotes: 0

Views: 1923

Answers (1)

nav
nav

Reputation: 440

I found what it was wrong. After looking at libbpf source coude, I found the variable

static const struct bpf_sec_def section_defs[] = {... BPF_PROG_SEC("xdp",BPF_PROG_TYPE_XDP), ... 

So, I noticed that I have not defined my XDP section in my BPF program with SEC("xdp").

Thanks @Qeole for your help.

Upvotes: 1

Related Questions