bpf_xdp_adjust_head 问题

问题描述

使用 bpf 开发一个小程序来扩展数据包(即:添加 mpls encap)。 设法扩展没有问题,但我写回标头 s_mac 和 d_mac 不正确。我假设是因为新空间较小,我与原始 eth 标头重叠,因此我得到了错误的值。

    /* extend the packet for mpls header encapsulation */
    if (bpf_xdp_adjust_head(ctx,0 - (int)sizeof(struct mpls_hdr)))
        return XDP_DROP;

    data = (void *)(long)ctx->data;
    data_end = (void *)(long)ctx->data_end;

    /* relocate ethernet header to start of packet and set MACs */
    new_eth = data;
    old_eth = data + (int)sizeof(struct mpls_hdr);
    //set new header while swaping src/dst mac
    set_ethhdr(new_eth,old_eth,bpf_htons(ETH_P_MPLS_UC));

我认为这就是问题发生的地方,当我 memcpy 到 new_eth 时,我正在咬入 old_eth 空间。有没有办法通过将 old_eth 的值复制到另一个结构中,然后使用该结构创建 new_eth 来解决这个问题?

static __always_inline void set_ethhdr(struct ethhdr *new_eth,const struct ethhdr *old_eth,__be16 h_proto)
{
    memcpy(new_eth->h_source,old_eth->h_source,ETH_ALEN);
    memcpy(new_eth->h_dest,old_eth->h_dest,ETH_ALEN);
    new_eth->h_proto = h_proto;
}

总的来说,我对 BPF 和 C 很陌生,所以不确定这是否确实是问题所在。

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)