0

所以我在我的 ebpf 程序中有这个代码,所以我的系统struct xdp_md确实存在。

struct xdp_md {
    __u32 data;
    __u32 data_end;
    __u32 data_meta;
    /* Below access go through struct xdp_rxq_info */
    __u32 ingress_ifindex; /* rxq->dev->ifindex */
    __u32 rx_queue_index;  /* rxq->queue_index  */

    __u32 egress_ifindex;  /* txq->dev->ifindex */
};

上面的结构在 /usr/include/linux/bpf.h 文件中

所以你可以看到 egree_ifindex 确实存在。但是当我编译时它说

 -O2 -emit-llvm -c -g -o af_xdp_kern.ll af_xdp_kern.c
af_xdp_kern.c:49:22: error: no member named 'egress_ifindex' in 'struct xdp_md'; did you mean 'ingress_ifindex'?
    int index = ctx->egress_ifindex;
                     ^~~~~~~~~~~~~~
                     ingress_ifindex
../headers/linux/bpf.h:2861:8: note: 'ingress_ifindex' declared here
        __u32 ingress_ifindex; /* rxq->dev->ifindex */
              ^
1 error generated.

以下是我在 ebpf 程序中的代码

SEC("xdp_devmap_xmit")
int xdp_sock_prog(struct xdp_md *ctx)
{
    int index = ctx->egress_ifindex
    __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;
}

谁能指导我获取 iphdr(用于出口数据包)究竟需要什么。这可能吗?

4

0 回答 0