我想为我的 AF-XDP 程序测量数据包延迟。我在看这个参考:https ://github.com/xdp-project/xdp-project/blob/master/areas/arm64/xdp_for_tsn.org
并将其调整为:
SEC("xdp_sock")
int xdp_sock_prog(struct xdp_md *ctx) {
int index = ctx->rx_queue_index;
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
const unsigned long long kstamp = bpf_ktime_get_ns();
if(data - sizeof(unsigned long long) <= data_end) {
ctx->data_meta = ctx->data - sizeof(unsigned long long);
memcpy(&ctx->data_meta, &kstamp, sizeof(unsigned long long));
}
...
}
然后在用户空间中的访问发生如下:
static bool process_packet(struct xsk_socket_info *xsk, uint64_t addr, uint32_t len) {
uint8_t *pkt = xsk_umem__get_data(xsk->umem->buffer, addr);
uint8_t *pkt_meta = xsk_umem__get_data(xsk->umem->buffer, addr - sizeof(unsigned long long));
const unsigned long long kstamp = (uint64_t) pkt_meta[7] << 56 | (uint64_t) pkt_meta[6] << 48 | (uint64_t) pkt_meta[5] << 40
| (uint64_t) pkt_meta[4] << 32 | (uint64_t) pkt_meta[3] << 24 | (uint64_t) pkt_meta[2] << 16
| (uint64_t) pkt_meta[1] << 8 | (uint64_t) pkt_meta[0];
...
}
但我无法将 XDP 程序加载到内核中:
Load XDP program...
libbpf: load bpf program failed: Permission denied
libbpf: -- BEGIN DUMP LOG ---
libbpf:
0: (bf) r6 = r1
1: (61) r1 = *(u32 *)(r6 +16)
2: (63) *(u32 *)(r10 -4) = r1
3: (61) r7 = *(u32 *)(r6 +4)
4: (61) r8 = *(u32 *)(r6 +0)
5: (85) call bpf_ktime_get_ns#5
6: (bf) r1 = r8
7: (07) r1 += -8
8: (2d) if r1 > r7 goto pc+3
R0_w=inv(id=0) R1_w=pkt(id=0,off=-8,r=0,imm=0) R6_w=ctx(id=0,off=0,imm=0) R7_w=pkt_end(id=0,off=0,imm=0) R8_w=pkt(id=0,off=0,r=0,imm=0) R10=fp0 fp-8=mmmm????
9: (63) *(u32 *)(r6 +8) = r0
invalid bpf_context access off=8 size=4
processed 10 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
libbpf: -- END LOG --
我的意思是,这不应该是可能的——对吧?因为 XDP 完全是关于有效的内存访问,为什么在定义的边界之外的访问会data
一直data_end
有效?
恕我直言,如果保持不变,它可以工作ctx->data_meta
- 但是我在用户空间遇到了问题,因为我不知道它data_meta
的位置。是否有任何 libbpf 辅助函数来访问数据包元数据?
也可能是访问是有效的,但我的范围检查是错误的......