This commit is contained in:
Pragyansh Chaturvedi
2026-09-25 10:26:22 +05:30
parent 3a984dd9e2
commit ad6adb14c9
18 changed files with 21 additions and 76 deletions

View File

@ -188,7 +188,7 @@ def trace_open(ctx: c_void_p) -> c_int64:
For network packet processing:
```python
from pythonbpf.helper import XDP_PASS
from vmlinux import XDP_PASS
@section("xdp")
def xdp_pass(ctx: c_void_p) -> c_int64:

View File

@ -108,7 +108,7 @@ def trace_open_return(ctx):
For network packet processing at the earliest point:
```python
from pythonbpf.helper import XDP_PASS
from vmlinux import XDP_PASS
from ctypes import c_void_p, c_int64
@section("xdp")

View File

@ -290,7 +290,8 @@ class MyStruct:
```python
from pythonbpf import bpf, struct, map, section
from pythonbpf.maps import RingBuffer
from pythonbpf.helper import ktime, XDP_PASS
from pythonbpf.helper import ktime
from vmlinux import XDP_PASS
from ctypes import c_void_p, c_int64, c_uint8, c_uint16, c_uint32, c_uint64
@bpf

View File

@ -1,5 +1,5 @@
from pythonbpf import bpf, map, section, bpfglobal, compile, compile_to_ir
from pythonbpf.helper import XDP_PASS
from vmlinux import XDP_PASS
from pythonbpf.maps import HashMap
from ctypes import c_int64, c_void_p

View File

@ -30,7 +30,7 @@ from pythonbpf.allocation_pass import (
LocalSymbol,
)
from .function_debug_info import generate_function_debug_info
from .return_utils import handle_none_return, handle_xdp_return, is_xdp_name
from .return_utils import handle_none_return
from .function_metadata import get_probe_string, is_global_function, infer_return_type
@ -341,20 +341,6 @@ def handle_return(
logger.info(f"Handling return statement: {ast.dump(stmt)}")
if stmt.value is None:
return handle_none_return(builder)
elif (
isinstance(stmt.value, ast.Name)
and is_xdp_name(stmt.value.id)
and stmt.value.id not in local_sym_tab
and (
compilation_context is None
or stmt.value.id not in compilation_context.bpf_globals
)
):
# The XDP fast path resolves names like XDP_PASS from the helper
# constant table, but only as a fallback: a local or @bpfglobal of the
# same name shadows it, mirroring C (a local shadows an enum constant)
# and the resolution order everywhere else in the compiler.
return handle_xdp_return(stmt, builder, ret_type)
else:
# Fallback for now if ctx not passed, but caller should pass it
if compilation_context is None:

View File

@ -1,44 +1,12 @@
import logging
import ast
from llvmlite import ir
logger: logging.Logger = logging.getLogger(__name__)
XDP_ACTIONS = {
"XDP_ABORTED": 0,
"XDP_DROP": 1,
"XDP_PASS": 2,
"XDP_TX": 3,
"XDP_REDIRECT": 4,
}
def handle_none_return(builder) -> bool:
"""Handle return or return None -> returns 0."""
builder.ret(ir.Constant(ir.IntType(64), 0))
logger.debug("Generated default return: 0")
return True
def is_xdp_name(name: str) -> bool:
"""Check if a name is an XDP action"""
return name in XDP_ACTIONS
def handle_xdp_return(stmt: ast.Return, builder, ret_type) -> bool:
"""Handle XDP returns"""
if not isinstance(stmt.value, ast.Name):
return False
action_name = stmt.value.id
if action_name not in XDP_ACTIONS:
raise ValueError(
f"Unknown XDP action: {action_name}. Available: {XDP_ACTIONS.keys()}"
)
value = XDP_ACTIONS[action_name]
builder.ret(ir.Constant(ret_type, value))
logger.debug(f"Generated XDP action return: {action_name} = {value}")
return True

View File

@ -18,8 +18,6 @@ from .helpers import (
skb_store_bytes,
get_current_cgroup_id,
get_stack,
XDP_DROP,
XDP_PASS,
)
@ -86,6 +84,4 @@ __all__ = [
"uid",
"skb_store_bytes",
"get_stack",
"XDP_DROP",
"XDP_PASS",
]

View File

@ -60,10 +60,3 @@ def get_stack(buf, flags=0):
def get_current_cgroup_id():
"""Get the current cgroup ID"""
return ctypes.c_int64(0)
XDP_ABORTED = ctypes.c_int64(0)
XDP_DROP = ctypes.c_int64(1)
XDP_PASS = ctypes.c_int64(2)
XDP_TX = ctypes.c_int64(3)
XDP_REDIRECT = ctypes.c_int64(4)

View File

@ -1,5 +1,5 @@
from pythonbpf import bpf, map, section, bpfglobal, compile
from pythonbpf.helper import XDP_PASS
from vmlinux import XDP_PASS
from pythonbpf.maps import HashMap
from ctypes import c_void_p, c_int64

View File

@ -1,5 +1,5 @@
from pythonbpf import bpf, map, section, bpfglobal, compile
from pythonbpf.helper import XDP_PASS
from vmlinux import XDP_PASS
from pythonbpf.maps import HashMap
from ctypes import c_void_p, c_int64

View File

@ -1,6 +1,6 @@
from pythonbpf import bpf, map, section, bpfglobal, compile_to_ir
from pythonbpf.maps import HashMap
from pythonbpf.helper import XDP_PASS
from vmlinux import XDP_PASS
from vmlinux import TASK_COMM_LEN # noqa: F401
from vmlinux import struct_qspinlock # noqa: F401

View File

@ -5,7 +5,7 @@
# program itself is the smallest privileged-type program there is.
from pythonbpf import bpf, section, bpfglobal, compile
from pythonbpf.helper import XDP_DROP
from vmlinux import XDP_DROP
from ctypes import c_void_p, c_int64

View File

@ -10,7 +10,7 @@
# return XDP_PASS;
from pythonbpf import bpf, section, bpfglobal, compile
from pythonbpf.helper import XDP_PASS
from vmlinux import XDP_PASS
from vmlinux import struct_xdp_md
from ctypes import c_int64

View File

@ -6,7 +6,7 @@
# for.
from pythonbpf import bpf, section, bpfglobal, compile
from pythonbpf.helper import XDP_PASS
from vmlinux import XDP_PASS
from ctypes import c_void_p, c_int64

View File

@ -3,8 +3,7 @@
# Bounce every packet back out of the interface it arrived on. Upstream is
# the transmit side of the veth XDP tests.
#
# XDP_TX comes from vmlinux because pythonbpf.helper exports only XDP_PASS
# and XDP_DROP; that is why this lives under vmlinux/.
# XDP actions are vmlinux enum constants, like every kernel constant.
from pythonbpf import bpf, section, bpfglobal, compile
from vmlinux import XDP_TX

View File

@ -1,7 +1,9 @@
# A local named after an XDP action must shadow the helper constant table,
# in return position too. clang agrees: a local legally shadows an enum
# constant, and the local's value is what returns (tests/c-form reference).
# Before the fix this returned the hardcoded 2 while XDP_PASS held 55.
# A local named after an XDP action is just a local: it shadows the vmlinux
# enum constant of that name (when vmlinux is imported) exactly as a local
# shadows an enum constant in C, and its value is what returns. This once
# went through a special-cased return path that ignored the local and
# returned the hardcoded 2 while XDP_PASS held 55; that path is gone, and
# return resolves names like every other expression.
from pythonbpf import bpf, section, bpfglobal, compile
from ctypes import c_void_p, c_int64

View File

@ -1,6 +1,6 @@
from pythonbpf import bpf, section, bpfglobal, compile
from ctypes import c_void_p, c_int64
from pythonbpf.helper import XDP_PASS
from vmlinux import XDP_PASS
@bpf

View File

@ -26,7 +26,7 @@
"failing_tests/vmlinux/args_test.py" = {reason = "struct_trace_event_raw_sys_enter args field access not supported", level = "ir"}
"failing_tests/xdp_pass.py" = {reason = "XDP program using vmlinux structs (struct_xdp_md) and complex map/struct interaction not yet supported", level = "ir"}
"failing_tests/vmlinux/xdp_pass.py" = {reason = "XDP program using vmlinux structs (struct_xdp_md) and complex map/struct interaction not yet supported", level = "ir"}
"failing_tests/globals_read_before_shadow.py" = {reason = "Reading a name above the assignment that makes it a local shadowing a global is UnboundLocalError in Python, and a compile error here", level = "ir"}