我尝试BPF_MAP_TYPE_XSKMAP
在我的用户空间程序中获取 fd。
这是代码:
ret = bpf_get_link_xdp_id(cfg->ifindex, &cfg->prog_id, cfg->xdp_flags);
if (ret) {
fprintf(stderr, "`bpf_get_link_xdp_id` returned error: %s\n", strerror(errno));
exit(-ret);
}
cfg->prog_fd = bpf_prog_get_fd_by_id(cfg->prog_id);
if(cfg->prog_fd < 0) {
fprintf(stderr, "Failed to obtain prog_fd: %s\n", strerror(errno));
exit(1);
}
struct bpf_prog_info prog_info;
uint32_t prog_info_len = sizeof(struct bpf_prog_info);
uint32_t map_info_len = sizeof(struct bpf_map_info);
if(bpf_obj_get_info_by_fd(cfg->prog_fd, &prog_info, &prog_info_len) == 0) {
const uint32_t amnt_of_maps = prog_info.nr_map_ids;
uint32_t *map_ids = (uint32_t*)malloc(sizeof(uint32_t) * prog_info.nr_map_ids);
memset(&prog_info, 0, prog_info_len);
prog_info.nr_map_ids = amnt_of_maps;
prog_info.map_ids = (uint64_t)(unsigned long)map_ids;
if(bpf_obj_get_info_by_fd(cfg->prog_fd, &prog_info, &prog_info_len) == 0) {
for(uint32_t i = 0; i < prog_info.nr_map_ids; i++) {
const int map_fd = bpf_map_get_fd_by_id(map_ids[i]);
if(map_fd >= 0) {
struct bpf_map_info map_info;
if(bpf_obj_get_info_by_fd(map_fd, &map_info, &map_info_len) == 0) {
if(strcmp(map_info.name, "xsks_map") == 0) {
cfg->xsks_map_fd = map_fd;
printf("Got fd of xsks_map!\n");
break;
}
} else {
close(map_fd);
}
} else {
fprintf(stderr, "Failed to obtain map_fd for id %d: %s\n", i, strerror(errno));
}
}
} else {
fprintf(stderr, "Failed to obtain prog_info 2: %s\n", strerror(errno));
}
} else {
fprintf(stderr, "Failed to obtain prog_info 1: %s\n", strerror(errno));
}
bpf_obj_get_info_by_fd(cfg->prog_fd, &prog_info, &prog_info_len)
但不幸的是,我在调用:时收到错误消息Failed to obtain prog_info 1: Invalid argument
。
我不知道这是不是因为 XDP 程序是从另一个进程加载的?同一个进程是否必须获取也加载了AF-XDP程序的程序信息?
编辑:我在用户空间程序中唯一更改的是填充一个BPF_MAP_TYPE_HASH
via bpf_map_update_elem
(条目在那里,用 观察bpftool map dump id <id>
)并bpf_map_lookup_elem
在我的 XDP 程序中使用(相同的哈希映射)。
突然,错误变为:
Failed to obtain prog_info 1: Bad address