From ad6adb14c92ecad37ca849b4301142767c74e610 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 25 Sep 2026 10:26:22 +0530 Subject: [PATCH] x --- docs/getting-started/quickstart.md | 2 +- docs/user-guide/decorators.md | 2 +- docs/user-guide/structs.md | 3 +- examples/xdp_pass.py | 2 +- pythonbpf/functions/functions_pass.py | 16 +--------- pythonbpf/functions/return_utils.py | 32 ------------------- pythonbpf/helper/__init__.py | 4 --- pythonbpf/helper/helpers.py | 7 ---- .../{ => vmlinux}/direct_assign.py | 2 +- .../failing_tests/{ => vmlinux}/named_arg.py | 2 +- tests/failing_tests/{ => vmlinux}/xdp_pass.py | 2 +- .../{xdp => vmlinux}/priv_prog.py | 2 +- .../vmlinux/xdp_devmap_helpers.py | 2 +- .../{xdp => vmlinux}/xdp_dummy.py | 2 +- .../{xdp => vmlinux}/xdp_tx.py | 3 +- .../passing_tests/return/xdp_name_shadowed.py | 10 +++--- .../{return/xdp.py => vmlinux/return_xdp.py} | 2 +- tests/test_config.toml | 2 +- 18 files changed, 21 insertions(+), 76 deletions(-) rename tests/failing_tests/{ => vmlinux}/direct_assign.py (96%) rename tests/failing_tests/{ => vmlinux}/named_arg.py (95%) rename tests/failing_tests/{ => vmlinux}/xdp_pass.py (97%) rename tests/kernel_selftest_equivalent/{xdp => vmlinux}/priv_prog.py (93%) rename tests/kernel_selftest_equivalent/{xdp => vmlinux}/xdp_dummy.py (94%) rename tests/kernel_selftest_equivalent/{xdp => vmlinux}/xdp_tx.py (78%) rename tests/passing_tests/{return/xdp.py => vmlinux/return_xdp.py} (88%) diff --git a/docs/getting-started/quickstart.md b/docs/getting-started/quickstart.md index 2283adf..b463478 100644 --- a/docs/getting-started/quickstart.md +++ b/docs/getting-started/quickstart.md @@ -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: diff --git a/docs/user-guide/decorators.md b/docs/user-guide/decorators.md index ff8b242..c884fc6 100644 --- a/docs/user-guide/decorators.md +++ b/docs/user-guide/decorators.md @@ -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") diff --git a/docs/user-guide/structs.md b/docs/user-guide/structs.md index 5c68c23..adee503 100644 --- a/docs/user-guide/structs.md +++ b/docs/user-guide/structs.md @@ -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 diff --git a/examples/xdp_pass.py b/examples/xdp_pass.py index ea294ff..1f49f03 100644 --- a/examples/xdp_pass.py +++ b/examples/xdp_pass.py @@ -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 diff --git a/pythonbpf/functions/functions_pass.py b/pythonbpf/functions/functions_pass.py index 382ef38..46fb71c 100644 --- a/pythonbpf/functions/functions_pass.py +++ b/pythonbpf/functions/functions_pass.py @@ -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: diff --git a/pythonbpf/functions/return_utils.py b/pythonbpf/functions/return_utils.py index a05c704..5979fa6 100644 --- a/pythonbpf/functions/return_utils.py +++ b/pythonbpf/functions/return_utils.py @@ -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 diff --git a/pythonbpf/helper/__init__.py b/pythonbpf/helper/__init__.py index bd4fe17..6a26dcf 100644 --- a/pythonbpf/helper/__init__.py +++ b/pythonbpf/helper/__init__.py @@ -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", ] diff --git a/pythonbpf/helper/helpers.py b/pythonbpf/helper/helpers.py index 253c4b0..832f16f 100644 --- a/pythonbpf/helper/helpers.py +++ b/pythonbpf/helper/helpers.py @@ -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) diff --git a/tests/failing_tests/direct_assign.py b/tests/failing_tests/vmlinux/direct_assign.py similarity index 96% rename from tests/failing_tests/direct_assign.py rename to tests/failing_tests/vmlinux/direct_assign.py index a784313..f3362b4 100644 --- a/tests/failing_tests/direct_assign.py +++ b/tests/failing_tests/vmlinux/direct_assign.py @@ -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 diff --git a/tests/failing_tests/named_arg.py b/tests/failing_tests/vmlinux/named_arg.py similarity index 95% rename from tests/failing_tests/named_arg.py rename to tests/failing_tests/vmlinux/named_arg.py index 19139df..bea5e22 100644 --- a/tests/failing_tests/named_arg.py +++ b/tests/failing_tests/vmlinux/named_arg.py @@ -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 diff --git a/tests/failing_tests/xdp_pass.py b/tests/failing_tests/vmlinux/xdp_pass.py similarity index 97% rename from tests/failing_tests/xdp_pass.py rename to tests/failing_tests/vmlinux/xdp_pass.py index c8510dc..b37a9b8 100644 --- a/tests/failing_tests/xdp_pass.py +++ b/tests/failing_tests/vmlinux/xdp_pass.py @@ -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 diff --git a/tests/kernel_selftest_equivalent/xdp/priv_prog.py b/tests/kernel_selftest_equivalent/vmlinux/priv_prog.py similarity index 93% rename from tests/kernel_selftest_equivalent/xdp/priv_prog.py rename to tests/kernel_selftest_equivalent/vmlinux/priv_prog.py index 181d684..976144f 100644 --- a/tests/kernel_selftest_equivalent/xdp/priv_prog.py +++ b/tests/kernel_selftest_equivalent/vmlinux/priv_prog.py @@ -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 diff --git a/tests/kernel_selftest_equivalent/vmlinux/xdp_devmap_helpers.py b/tests/kernel_selftest_equivalent/vmlinux/xdp_devmap_helpers.py index 1e9de0a..771b017 100644 --- a/tests/kernel_selftest_equivalent/vmlinux/xdp_devmap_helpers.py +++ b/tests/kernel_selftest_equivalent/vmlinux/xdp_devmap_helpers.py @@ -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 diff --git a/tests/kernel_selftest_equivalent/xdp/xdp_dummy.py b/tests/kernel_selftest_equivalent/vmlinux/xdp_dummy.py similarity index 94% rename from tests/kernel_selftest_equivalent/xdp/xdp_dummy.py rename to tests/kernel_selftest_equivalent/vmlinux/xdp_dummy.py index 82686de..c715cac 100644 --- a/tests/kernel_selftest_equivalent/xdp/xdp_dummy.py +++ b/tests/kernel_selftest_equivalent/vmlinux/xdp_dummy.py @@ -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 diff --git a/tests/kernel_selftest_equivalent/xdp/xdp_tx.py b/tests/kernel_selftest_equivalent/vmlinux/xdp_tx.py similarity index 78% rename from tests/kernel_selftest_equivalent/xdp/xdp_tx.py rename to tests/kernel_selftest_equivalent/vmlinux/xdp_tx.py index 8d40ffe..d432eb7 100644 --- a/tests/kernel_selftest_equivalent/xdp/xdp_tx.py +++ b/tests/kernel_selftest_equivalent/vmlinux/xdp_tx.py @@ -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 diff --git a/tests/passing_tests/return/xdp_name_shadowed.py b/tests/passing_tests/return/xdp_name_shadowed.py index 0caacd8..b188dd2 100644 --- a/tests/passing_tests/return/xdp_name_shadowed.py +++ b/tests/passing_tests/return/xdp_name_shadowed.py @@ -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 diff --git a/tests/passing_tests/return/xdp.py b/tests/passing_tests/vmlinux/return_xdp.py similarity index 88% rename from tests/passing_tests/return/xdp.py rename to tests/passing_tests/vmlinux/return_xdp.py index 3c0f5d8..3978c72 100644 --- a/tests/passing_tests/return/xdp.py +++ b/tests/passing_tests/vmlinux/return_xdp.py @@ -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 diff --git a/tests/test_config.toml b/tests/test_config.toml index 1f01001..ada07a1 100644 --- a/tests/test_config.toml +++ b/tests/test_config.toml @@ -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"}