C - 读取用户空间中 EBPF BPF_MAP_TYPE_HASH的更新值 |可以使用 read() linux 函数来获取通过 map 共享的对象的当前值吗?



所以我有一个内核ebpf程序,它将xdp钩子连接到接口eno1,其中有一个类型为BPF_map_type_HASH的映射ip_map,我正在与用户空间共享。因此,在我的用户空间中,我得到了映射的文件描述符,但现在我喜欢得到的更新值struct share_me,我在我的用户空间加载器程序中的BPF_MAP_TYPE_HASH类型映射的帮助下共享它。

任何人都请帮我解释一下,关于我如何做到这一点,

因此,我假设如果我的map_fd指向BPF_MAP_TYPE_HASH指向我的MAP,那么我可以执行int sizeof_share_me_read=read(map_fd,&share_me,sizeof(struct share_me));,这样我就可以读取从内核ebpf程序共享的map_fd(BPF_MAP_TYPE_HASH)的当前更新值,该值将在share_me对象的struct iphdr dest_ip成员中包含当前数据包的ip头。有谁能帮我整理一下吗

user.c

/* SPDX-License-Identifier: GPL-2.0 */
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <locale.h>
#include <poll.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/resource.h>
#include <bpf/bpf.h>
#include <bpf/xsk.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <linux/if_link.h>
#include <linux/if_ether.h>
#include <linux/ipv6.h>
#include <linux/icmpv6.h>

#include "../common/common_params.h"
#include "../common/common_user_bpf_xdp.h"
#include "../common/common_libbpf.h"

int main()
{
struct bpf_object *bpf_obj;
struct bpf_program *bpf_prog;
bpf_obj = load_bpf_object_file("af_xdp_kern.o", 0);
if (!bpf_obj) {
printf("ERR: loading file:n");
exit(EXIT_FAIL_BPF);
}
bpf_prog = bpf_program__next(NULL, bpf_obj);
if (!bpf_prog)
{
printf("bpf_program_next:n");
exit(0);    
}
int prog_fd = bpf_program__fd(bpf_prog);
if (prog_fd <= 0) {
}
int err = xdp_link_attach(if_nametoindex("eno1"), 0, prog_fd);
if(err)
{
printf("xdp_link_attachn");
exit(0);
}   
struct bpf_map *map1 = bpf_object__find_map_by_name(bpf_obj, "ip_map");
int map_fd = bpf_map__fd(map1);
if(map_fd<0)
{
printf("map fd <0n");
exit(0);
}

return 0;
}

kernel.c

/* SPDX-License-Identifier: GPL-2.0 */
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/udp.h>
void *memcpy(void *, const void *, unsigned long);
int IPPROTO_UDP = 17;
struct share_me
{
struct iphdr dest_ip;
};
struct bpf_map_def SEC("maps") ip_map = {
.type = BPF_MAP_TYPE_HASH,
.key_size = sizeof(int),
.value_size = sizeof(struct share_me),
.max_entries = 64,  /* Assume netdev has no more than 64 queues */
};
struct bpf_map_def SEC("maps") xsks_map = {
.type = BPF_MAP_TYPE_XSKMAP,
.key_size = sizeof(int),
.value_size = sizeof(int),
.max_entries = 64,  /* Assume netdev has no more than 64 queues */
};

struct bpf_map_def SEC("maps") xdp_stats_map = {
.type        = BPF_MAP_TYPE_PERCPU_ARRAY,
.key_size    = sizeof(int),
.value_size  = sizeof(__u32),
.max_entries = 64,
};
SEC("xdp_sock")
int xdp_sock_prog(struct xdp_md *ctx)
{
int index = ctx->rx_queue_index;
__u32 *pkt_count;
void *data = (void *)(long)ctx->data;
void *data_end = (void *)(long)ctx->data_end;
struct ethhdr *eth = data;
struct share_me me;
if ((void *)eth + sizeof(*eth) <= data_end)
{
struct iphdr *ip = data + sizeof(*eth);
//me.dest_ip=ip;

if(((void *)ip+sizeof(*ip))<=data_end)
{   
struct iphdr ip_temp=(struct iphdr)*ip;            
memcpy(&me.dest_ip,&ip_temp,sizeof(ip_temp));
bpf_map_lookup_elem(&ip_map, &index);
bpf_map_update_elem(&ip_map,&index,&me,0);
if ((void *)ip + sizeof(*ip) <= data_end)
{
if (ip->protocol == IPPROTO_UDP)
{
struct udphdr *udp = (void *)ip + sizeof(*ip);
if ((void *)udp + sizeof(*udp) <= data_end)
{
//u64 value = htons(udp->dest);
//counter.increment(value);
}
}
}
}  
}

pkt_count = bpf_map_lookup_elem(&xdp_stats_map, &index);
if (pkt_count) {
/* We pass every other packet */
if ((*pkt_count)++ & 1)
return XDP_DROP;
}
/* A set entry here means that the correspnding queue_id
* has an active AF_XDP socket bound to it. */
if (bpf_map_lookup_elem(&xsks_map, &index))
return bpf_redirect_map(&xsks_map, index, 0);
return XDP_PASS;
}
char _license[] SEC("license") = "GPL";

现在您有了映射fd,您需要使用libbpf的bpf_map_lookup_elem函数来读取值:

int key = 0;
struct share_me value = {};
if (bpf_map_lookup_elem(map_fd, &key, &value))
// Print errno.

最新更新