在加载了ebpf程序和xsk_socket__create()成功之后,ebpf程序中的BPF_MAP_TYPE_XSKMAP数组中的元素仍未设置。流量由ebpf程序捕获,但不重定向到用户空间套接字。
我正在使用linux-5.17.8,并从内核源代码构建libbpf和bpftool。所述ebpf程序被加载并链接到所述接口;
bpftool prog loadall ./xdp_kern.o /sys/fs/bpf/xdptest pinmaps /sys/fs/bpf/xdptest
bpftool net attach xdpdrv pinned /sys/fs/bpf/xdptest/xdp_prog_redirect dev eth1只有通道0,Q=0在xsk_socket__create()调用中使用(因此这不是XDP文档中描述的“侦听错误通道”问题)。
流量被捕获,我在内核程序中打印出来;
#define Dx(fmt, ...) \
({ \
char ____fmt[] = fmt; \
bpf_trace_printk(____fmt, sizeof(____fmt), ##__VA_ARGS__); \
})
...
int index = ctx->rx_queue_index;
Dx("Q=%2d %s", index, bpf_map_lookup_elem(&xsks_map, &index) ? "AF_XDP" : "-");这样我就可以验证元素是否为空。
必须从linux-5.13调用xsk_socket__create();
xsk_cfg.libbpf_flags = XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD;否则你会得到;
libbpf: Netlink-based XDP prog detected, please unload it in order to launch AF_XDP prog
Failed xsk_socket__create (ingress); Invalid argument有人能说出我在这里做错了什么吗?
既然xsk_socket__create()成功了,我假设某个地方已经填充了一些BPF_MAP_TYPE_XSKMAP,而不是我的ebpf程序中的那个。我试着用新旧两种方式来宣告它;
// https://github.com/libbpf/libbpf/wiki/Libbpf:-the-road-to-v1.0#drop-support-for-legacy-bpf-map-declaration-syntax
// Socket map for redirect to user-space
#if 0
struct bpf_map_def SEC("maps") xsks_map = {
.type = BPF_MAP_TYPE_XSKMAP,
//.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(int),
.max_entries = 16, /* Must be > nqueues for the nic */
};
#else
struct {
__uint(type, BPF_MAP_TYPE_XSKMAP);
__uint(max_entries, 16);
__type(key, int);
__type(value, int);
} xsks_map SEC("maps");
#endif发布于 2022-06-15 09:54:26
bpf自5.13以来发生了很多变化,所以以前起作用的东西可能不再起作用了。xdp-教程也没有被更新以使用最新的内核版本,因此没有太多的示例可供使用。
对于这个特殊的问题,它看起来是API接口的想法。但是,在进行更改后,如果支持可用,它似乎只倾向于使用bpf_link*,即使您已经通过"bpftool“加载了XDP程序,回到使用bpftool加载的XDP程序也是很好的选择。因此,最好使用bpf_link。或者,如果您是从内核源构建的,下面的修补程序可能会有所帮助。
Subject: [PATCH] Allow bpf_link and netlink based program loading at the same
time
---
tools/lib/bpf/xsk.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/tools/lib/bpf/xsk.c b/tools/lib/bpf/xsk.c
index af136f73b09d..597d7102b25a 100644
--- a/tools/lib/bpf/xsk.c
+++ b/tools/lib/bpf/xsk.c
@@ -861,7 +861,8 @@ static int __xsk_setup_xdp_prog(struct xsk_socket *_xdp, int *xsks_map_fd)
if (ctx->has_bpf_link)
err = xsk_link_lookup(ctx->ifindex, &prog_id, &ctx->link_fd);
- else
+
+ if (!prog_id)
err = bpf_get_link_xdp_id(ctx->ifindex, &prog_id, xsk->config.xdp_flags);
if (err)
--
2.34.1https://stackoverflow.com/questions/72329171
复制相似问题