On Wed, Nov 27, 2024 at 12:54:41PM -0800, Martin KaFai Lau wrote: > On 11/27/24 11:07 AM, Maciej Fijalkowski wrote: > > But kfunc does not work on PTR_TO_CTX - it takes in directly sk_buff, not > > __sk_buff. As I mention above we use bpf_cast_to_kern_ctx() and per my > > current limited understanding it overwrites the reg->type to > > PTR_TO_BTF_ID | PTR_TRUSTED. > > Can you try skip calling the bpf_cast_to_kern_ctx and directly pass the > "struct __sk_buff *skb" to the "struct sk_buff *bpf_skb_acquire(struct > __sk_buff *skb). That didn't help, log below: libbpf: prog 'bpf_tc_ingress': -- BEGIN PROG LOAD LOG -- 0: R1=ctx() R10=fp0 ; map_entry = bpf_map_lookup_elem(&skb_map, &get_key); @ bpf_bpf.c:145 0: (18) r6 = 0xffffc9000d186004 ; R6_w=map_value(map=bpf_bpf.bss,ks=4,vs=8,off=4) 2: (18) r1 = 0xffff88984ab8ce00 ; R1_w=map_ptr(map=skb_map,ks=4,vs=8) 4: (18) r2 = 0xffffc9000d186004 ; R2_w=map_value(map=bpf_bpf.bss,ks=4,vs=8,off=4) 6: (85) call bpf_map_lookup_elem#1 ; R0_w=map_value_or_null(id=1,map=skb_map,ks=4,vs=8) ; if (!map_entry) { @ bpf_bpf.c:146 7: (55) if r0 != 0x0 goto pc+7 15: R0_w=map_value(map=skb_map,ks=4,vs=8) R6_w=map_value(map=bpf_bpf.bss,ks=4,vs=8,off=4) R10=fp0 ; tmp = bpf_kptr_xchg(&map_entry->skb, tmp); @ bpf_bpf.c:151 15: (bf) r1 = r0 ; R0_w=map_value(map=skb_map,ks=4,vs=8) R1_w=map_value(map=skb_map,ks=4,vs=8) 16: (b7) r2 = 0 ; R2_w=0 17: (85) call bpf_kptr_xchg#194 ; R0_w=ptr_or_null_sk_buff(id=3,ref_obj_id=3) refs=3 18: (bf) r6 = r0 ; R0_w=ptr_or_null_sk_buff(id=3,ref_obj_id=3) R6_w=ptr_or_null_sk_buff(id=3,ref_obj_id=3) refs=3 19: (b4) w7 = -1 ; R7=0xffffffff refs=3 ; if (!tmp) @ bpf_bpf.c:152 20: (15) if r6 == 0x0 goto pc+17 ; R6=ptr_sk_buff(ref_obj_id=3) refs=3 ; bpf_printk("retrieved skb %p from index %d\n", tmp, get_key); @ bpf_bpf.c:155 21: (18) r8 = 0xffffc9000d186004 ; R8_w=map_value(map=bpf_bpf.bss,ks=4,vs=8,off=4) refs=3 23: (61) r4 = *(u32 *)(r8 +0) ; R4_w=scalar(smin=0,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff)) R8_w=map_value(map=bpf_bpf.bss,ks=4,vs=8,off=4) refs=3 24: (18) r1 = 0xffff88984ab8cb20 ; R1_w=map_value(map=bpf_bpf.rodata,ks=4,vs=83,off=24) refs=3 26: (b4) w2 = 32 ; R2_w=32 refs=3 27: (bf) r3 = r6 ; R3_w=ptr_sk_buff(ref_obj_id=3) R6=ptr_sk_buff(ref_obj_id=3) refs=3 28: (85) call bpf_trace_printk#6 ; R0_w=scalar() refs=3 ; get_key++; @ bpf_bpf.c:157 29: (61) r1 = *(u32 *)(r8 +0) ; R1_w=scalar(smin=0,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff)) R8_w=map_value(map=bpf_bpf.bss,ks=4,vs=8,off=4) refs=3 30: (04) w1 += 1 ; R1_w=scalar(smin=0,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff)) refs=3 31: (b4) w7 = 0 ; R7_w=0 refs=3 32: (b4) w2 = 0 ; R2=0 refs=3 ; if (get_key >= 16) @ bpf_bpf.c:158 33: (26) if w1 > 0xf goto pc+1 ; R1=scalar(smin=smin32=0,smax=umax=smax32=umax32=15,var_off=(0x0; 0xf)) refs=3 34: (bc) w2 = w1 ; R1=scalar(id=4,smin=smin32=0,smax=umax=smax32=umax32=15,var_off=(0x0; 0xf)) R2_w=scalar(id=4,smin=smin32=0,smax=umax=smax32=umax32=15,var_off=(0x0; 0xf)) refs=3 35: (63) *(u32 *)(r8 +0) = r2 ; R2_w=scalar(id=4,smin=smin32=0,smax=umax=smax32=umax32=15,var_off=(0x0; 0xf)) R8=map_value(map=bpf_bpf.bss,ks=4,vs=8,off=4) refs=3 ; bpf_skb_release((struct __sk_buff *)tmp); @ bpf_bpf.c:161 36: (bf) r1 = r6 ; R1_w=ptr_sk_buff(ref_obj_id=3) R6=ptr_sk_buff(ref_obj_id=3) refs=3 37: (85) call bpf_skb_release#102037 arg#0 expected pointer to ctx, but got ptr_ processed 34 insns (limit 1000000) max_states_per_insn 0 total_states 3 peak_states 3 mark_read 1 -- END PROG LOAD LOG -- Still the same problem. Also even it would work it was not very convenient to cast these types back and forth...I then tried to store __sk_buff as kptr but I ended up with: "map 'skb_map' has to have BTF in order to use bpf_kptr_xchg" which got me lost:) I have a solution though which I'd like to discuss. > > Please share the patch and the test case. It will be easier for others to help. I have come up with rather simple way of achieving what I desired when starting this thread, how about something like this: