![](/img/trans.png)
[英]BPF / XDP: 'bpftool batch file' returns 'Error: reading batch file failed: Operation not permitted'
[英]BPF program using XDP returns failed to load BPF skeleton (-22)
首先,我沒有直接使用libbpf API,也沒有直接使用 BCC。 相反,我試圖使用bpftool生成的骨架的 API。
控制代碼:
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代碼:
// 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;
}
因此,運行后,顯示以下錯誤:
// 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
調試后,我注意到程序類型的值不正確。 它總是返回 0。因此,我必須在加載調用之前定義以下代碼:
obj_gen->progs.xdp_prog1->type = BPF_PROG_TYPE_XDP;
因為struct bpf_program
在libbpf.c
,我必須在我的頭文件中重新定義以便編譯找到它。 此解決方法有效。
問:有沒有更好的解決辦法?
我發現哪里不對勁了。 查看libbpf源代碼后,我找到了變量
static const struct bpf_sec_def section_defs[] = {... BPF_PROG_SEC("xdp",BPF_PROG_TYPE_XDP), ...
所以,我注意到我沒有在我的 BPF 程序中用SEC("xdp")
定義我的 XDP 部分。
感謝@Qeole的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.