mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 18:08:36 +00:00
xdp_md.data is a u32 in C, but the verifier tracks it as a packet pointer and rejects 32-bit arithmetic on it: "R0 32-bit pointer arithmetic prohibited", the same for a C program, which is why C casts it through (void *)(long) first. The u32 ranking the test pins is the correct one; it just has to be pinned on a field that is a scalar to the verifier too. ingress_ifindex is. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
30 lines
880 B
Python
30 lines
880 B
Python
# A sub-register context field is loaded widened to i64, but C ranks it by
|
|
# its declared width: xdp_md.ingress_ifindex is a u32, so ifindex - k with a
|
|
# c_int32 k is a u32 subtraction (the result is cut to 32 bits and
|
|
# zero-extended), not a 64-bit one.
|
|
#
|
|
# Not xdp_md.data: it is a u32 in C too, but the verifier tracks it as a
|
|
# packet pointer and rejects 32-bit arithmetic on it ("R0 32-bit pointer
|
|
# arithmetic prohibited"), for C programs as much as for this one. That is
|
|
# why C casts it through (void *)(long) before doing anything with it.
|
|
from ctypes import c_int32, c_int64
|
|
from pythonbpf import bpf, section, bpfglobal, compile
|
|
from vmlinux import struct_xdp_md
|
|
|
|
|
|
@bpf
|
|
@section("xdp")
|
|
def prog(ctx: struct_xdp_md) -> c_int64:
|
|
k = c_int32(1)
|
|
d = ctx.ingress_ifindex - k
|
|
return c_int64(d)
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def LICENSE() -> str:
|
|
return "GPL"
|
|
|
|
|
|
compile()
|