Unable to attach so_reuseport_cbpf in cpp.

huangapple go评论73阅读模式
英文:

Not able to attach so_reuseport_cbpf in cpp

问题

以下BPF程序在cpp中绑定到UDP套接字时使用SO_ATTACH_REUSEPORT_CBPF引发错误

struct sock_filter code[] = {
            {BPF_LD | BPF_MEM, 0, 0, 0x00000000}, // 将内存加载到累加器中
            //{BPF_ALU | BPF_ADD, 0, 0, 0x00000001}, // 添加1
            //{BPF_ALU | BPF_MOD, 0, 0, 0x00000003}, // 取模3
            //{BPF_ST , 0, 0, 0x00000000},    // 保存到临时内存
            //{BPF_RET | BPF_A, 0, 0, 0x00000000}, // 返回累加器中的值
            };

但是当我将BPF程序更改为

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

我的代码正常运行。

第一个BPF程序有什么问题?

我尝试执行两个BPF程序,期望第一个BPF程序成功附加到我的UDP套接字。

英文:

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.

答案1

得分: 0

所有可能的控制流都必须以BPF_RET操作结束。

以下内容也应有效:

struct sock_filter code[] = {
    {BPF_LD | BPF_MEM, 0, 0, 0x00000000}, // 将内存加载到累加器
    //{BPF_ALU | BPF_ADD, 0, 0, 0x00000001}, // 加1
    //{BPF_ALU | BPF_MOD, 0, 0, 0x00000003}, // 取模3
    //{BPF_ST , 0, 0, 0x00000000},    // 保存到临时存储器
    {BPF_RET | BPF_A, 0, 0, 0x00000000}, // 将累加器中的值返回
};
英文:

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
            };

答案2

得分: 0

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

Initially, it was

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

So that's why the BPF program

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

runs 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,
};
英文:

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,
    };

huangapple
  • 本文由 发表于 2023年6月13日 15:47:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定