问题描述
我开始用 ebpf 和 XDP 编码。 我正在使用 python bcc 将 XDP 程序加载到 NIC。 我正在尝试使用 __sk_buff 结构,但是当我尝试访问 skb 的任何字段时,验证程序无法加载程序。
int xdp_test(struct sk_buff *skb)
{
void *data = (void*)(long)skb->data;
void *data_end = (void*)(long)skb->data_end;
if (data + sizeof(struct ethhdr) + sizeof(struct iphdr) < data_end)
{
struct iphdr * ip = ip_hdr(skb);
// according to my checks,it Failed because of this line. I cant access to ip->protocol (or any other fileds)
if (ip->protocol == IPPROTO_TCP)
{
return XDP_PASS;
}
}
...
return XDP_PASS
}
我只想使用 bpf_l4_csum_replace
在我的程序中计算第 4 层校验和,其中将 skb 作为第一个参数。
为什么会这样? 我什至可以在 XDP 中使用 __sk_buff 结构吗?或者我必须使用 xdp_md 结构?
更新: 感谢 Qeole,我明白我不能使用 XDP 使用 sk_buff。 有没有一种方法可以使用 xdp_md 计算 TCP 校验和?
解决方法
确实,您不能使用 XDP 程序中的 struct __sk_buff
。您必须改用 struct xdp_md
。 XDP 的性能很大程度上归功于内核在分配和初始化套接字缓冲区(内核中的 struct sk_buff
)之前调用 eBPF 程序,这样既节省了时间和资源,也意味着您无法在 XDP 程序中访问该结构。