iovisor / bcc

BCC - Tools for BPF-based Linux IO analysis, networking, monitoring, and more
Apache License 2.0
20.58k stars 3.88k forks source link

BPF_SNPRINTF is not working #4057

Open huchen2021 opened 2 years ago

huchen2021 commented 2 years ago

When I use BPF_SNPRINTF on my xdp.c, it reports the following error. If I don’t use BPF_SNPRINTF, the error is gone. Any hints will be appreaciated:


go run -exec sudo ./pkg ens160
2022/06/17 05:04:56 loading objects: field XdpProgFunc: program xdp_prog_func: load program: permission denied: ; int xdp_prog_func(struct xdp_md *ctx)
0: (b7) r0 = 0
; void *data_end = (void *)(long)ctx->data_end;
1: (61) r9 = *(u32 *)(r1 +4)
; void *data = (void *)(long)ctx->data;
2: (61) r6 = *(u32 *)(r1 +0)
; if (data + sizeof(struct ethhdr) > data_end)
3: (bf) r1 = r6
4: (07) r1 += 14
; if (data + sizeof(struct ethhdr) > data_end)
5: (2d) if r1 > r9 goto pc+693
 R0_w=inv0 R1_w=pkt(id=0,off=14,r=14,imm=0) R6_w=pkt(id=0,off=0,r=14,imm=0) R9_w=pkt_end(id=0,off=0,imm=0) R10=fp0
; if (ethh->h_proto != bpf_htons(ETH_P_IP))
6: (71) r1 = *(u8 *)(r6 +12)
7: (71) r2 = *(u8 *)(r6 +13)
8: (67) r2 <<= 8
9: (4f) r2 |= r1
10: (b7) r0 = 2
; if (ethh->h_proto != bpf_htons(ETH_P_IP))
11: (55) if r2 != 0x8 goto pc+687
 R0_w=inv2 R1_w=inv(id=0,umax_value=255,var_off=(0x0; 0xff)) R2_w=inv8 R6_w=pkt(id=0,off=0,r=14,imm=0) R9_w=pkt_end(id=0,off=0,imm=0) R10=fp0
; if (data + sizeof(struct ethhdr) + sizeof(struct iphdr) > data_end)
12: (bf) r7 = r6
13: (07) r7 += 34
14: (b7) r0 = 0
; if (data + sizeof(struct ethhdr) + sizeof(struct iphdr) > data_end)
15: (2d) if r7 > r9 goto pc+683
 R0=inv0 R1=inv(id=0,umax_value=255,var_off=(0x0; 0xff)) R2=inv8 R6=pkt(id=0,off=0,r=34,imm=0) R7=pkt(id=0,off=34,r=34,imm=0) R9=pkt_end(id=0,off=0,imm=0) R10=fp0
; if (iph->protocol != IPPROTO_TCP)
16: (71) r1 = *(u8 *)(r6 +23)
17: (b7) r0 = 2
; if (iph->protocol != IPPROTO_TCP)
18: (55) if r1 != 0x6 goto pc+680
 R0_w=inv2 R1_w=inv6 R2=inv8 R6=pkt(id=0,off=0,r=34,imm=0) R7=pkt(id=0,off=34,r=34,imm=0) R9=pkt_end(id=0,off=0,imm=0) R10=fp0
; if (data + sizeof(struct ethhdr) + sizeof(struct iphdr) + sizeof(struct tcphdr) > data_end)
19: (bf) r1 = r6
20: (07) r1 += 54
21: (b7) r0 = 0
; if (data + sizeof(struct ethhdr) + sizeof(struct iphdr) + sizeof(struct tcphdr) > data_end)
22: (2d) if r1 > r9 goto pc+676
 R0_w=inv0 R1_w=pkt(id=0,off=54,r=54,imm=0) R2=inv8 R6=pkt(id=0,off=0,r=54,imm=0) R7=pkt(id=0,off=34,r=54,imm=0) R9=pkt_end(id=0,off=0,imm=0) R10=fp0
23: (b7) r1 = 0
; char ip_str[18] = {};
24: (6b) *(u16 *)(r10 -8) = r1
last_idx 24 first_idx 15
regs=2 stack=0 before 23: (b7) r1 = 0
25: (7b) *(u64 *)(r10 -16) = r1
26: (7b) *(u64 *)(r10 -24) = r1
27: (bf) r1 = r10
28: (07) r1 += -24
29: (bf) r4 = r10
30: (07) r4 += -40
; BPF_SNPRINTF(ip_str, sizeof(ip_str), "hello");
31: (b7) r2 = 18
32: (18) r3 = 0xffff8a264ae74923
34: (b7) r5 = 0
35: (85) call bpf_snprintf#165
last_idx 35 first_idx 15
regs=4 stack=0 before 34: (b7) r5 = 0
regs=4 stack=0 before 32: (18) r3 = 0xffff8a264ae74923
regs=4 stack=0 before 31: (b7) r2 = 18
R3 does not point to a readonly map'
processed 35 insns (limit 1000000) max_states_per_insn 0 total_states 1 peak_states 1 mark_read 1
ljluestc commented 1 day ago

#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

SEC("xdp")
int xdp_prog_func(struct xdp_md *ctx) {
    char ip_str[18] = {};
    const char fmt[] = "hello";

    bpf_snprintf(ip_str, sizeof(ip_str), fmt);

    // Additional code logic

    return XDP_PASS;
}

char _license[] SEC("license") = "GPL";