简体   繁体   English

即使成功加载了 bpf 代码,ebpf 尾调用也不起作用

[英]ebpf tail call didn't work even bpf code is loaded successfully

#include "bpf/bpf_helpers.h"
#include <linux/bpf.h>
char _license[] SEC("license") = "GPL";
struct bpf_map_def SEC("maps") jump_table = {
    .type = BPF_MAP_TYPE_PROG_ARRAY,
    .key_size = sizeof(__u32),
    .value_size = sizeof(__u32),
    .max_entries = 100,
};

SEC("xdp_1")
int test_func(struct xdp_md *ctx) {
  bpf_printk("tail call\n");
  return XDP_PASS;
}

SEC("xdp")
int xdp_pass_func(struct xdp_md *ctx) {
  __u32 zero = 0;
  bpf_tail_call(ctx, &jump_table, zero);
  bpf_printk("tail call failed\n");
  return XDP_PASS;
}

when i look cat /sys/kernel/debug/tracing/trace_pipe , it shows tail call failed ,but i don't know whats wrong,here is my load code当我查看cat /sys/kernel/debug/tracing/trace_pipe时,它​​显示tail call failed ,但我不知道出了什么问题,这是我的加载代码

func main() {
    if err := rlimit.RemoveMemlock(); err != nil {
        log.Fatal(err)
        return
    }
    var obj aclObjects
    err := loadAclObjects(&obj, nil)
    if err != nil {
        log.Fatal(err)
        return
    }
    err = obj.JumpTable.Put(uint32(0), uint32(obj.TestFunc.FD()))
    if err != nil {
        log.Fatal(err)
        return
    }
    link, err := netlink.LinkByName("ens33")
    if err != nil {
        log.Fatal(err)
        return
    }
    err = netlink.LinkSetXdpFd(link, obj.XdpPassFunc.FD())
    if err != nil {
        log.Fatal(err)
        return
    }
}

the bpf code can be loaded, but it seems some thing wrong with tail_call,i wrote it according to linux source code,someone can help me? bpf代码可以加载,但是tail_call好像有问题,我是按照linux源码写的,有人能帮帮我吗?

As you realised and mentioned in your answer, the file descriptor referencing the program will indeed be closed when the loader exits.正如您在回答中意识到并提到的那样,当加载程序退出时,引用程序的文件描述符确实会关闭。

If nothing else references the program, it is unloaded.如果没有其他内容引用该程序,则将其卸载。 What can hold such a reference?有什么可以持有这样的参考?

What you want is to pin your program before your loader exits (It seems you use goebpf? Apparently the library has a Pin() function , which should probably help).您想要的是在加载程序退出之前固定您的程序(您似乎使用 goebpf?显然该库有一个Pin()函数,这可能会有所帮助)。

我发现加载程序退出时prog fd会关闭的问题,所以我保持程序阻塞并且尾调用工作正常,我不知道为什么fd关闭是一个问题,希望有人能回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM