Not able to attach so_reuseport_cbpf in cpp

47 views Asked by At

Below BPF prgram in cpp is throwing error when binding to udp socket using SO_ATTACH_REUSEPORT_CBPF

struct sock_filter code[] = {
            {BPF_LD | BPF_MEM, 0, 0, 0x00000000}, // load memory to accumulator
            //{BPF_ALU | BPF_ADD, 0, 0, 0x00000001}, // add 1
            //{BPF_ALU | BPF_MOD, 0, 0, 0x00000003}, //module 3
            //{BPF_ST , 0, 0, 0x00000000},    // save in scratch memory
            //{BPF_RET | BPF_A, 0, 0, 0x00000000}, // return value in accumulator
            };

But when i am changing bpf program to

struct sock_filter code[] = {
            {BPF_RET, 0, 0, 0x00000000}
            };

My code is running fine.

What is problem in 1st bpf program??

I tried executing with both bpf program, expecting that first bpf program attach succesfully to my udp socket.

2

There are 2 answers

0
Upendra Kumar On BEST ANSWER

Both the BPF program is valid. I made mistake when declaring struct sock_fprog.

Initially it was

struct sock_fprog bpf = {
    .len = 1,
    .filter = code,
};

So that's why bpf program

struct sock_filter code[] = {
        {BPF_RET, 0, 0, 0x00000000}
        };

run correctly as it has only one instruction.

Now i changed the struct code to run for any number of instructions:

struct sock_fprog bpf = {
        .len = sizeof(code)/sizeof(code[0]),
        .filter = code,
    };
0
Dylan Reimerink On

All possible control flows must end with a BPF_RET operation.

The following should also be valid:

struct sock_filter code[] = {
            {BPF_LD | BPF_MEM, 0, 0, 0x00000000}, // load memory to accumulator
            //{BPF_ALU | BPF_ADD, 0, 0, 0x00000001}, // add 1
            //{BPF_ALU | BPF_MOD, 0, 0, 0x00000003}, //module 3
            //{BPF_ST , 0, 0, 0x00000000},    // save in scratch memory
            {BPF_RET | BPF_A, 0, 0, 0x00000000}, // return value in accumulator
            };