Skip to content

Commit 6f02b61

Browse files
committed
Add _handle_xdp_return
1 parent a21ff56 commit 6f02b61

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

pythonbpf/functions/functions_pass.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pythonbpf.binary_ops import handle_binary_op
1010
from pythonbpf.expr_pass import eval_expr, handle_expr
1111

12-
from .return_utils import _handle_none_return
12+
from .return_utils import _handle_none_return, _handle_xdp_return
1313

1414

1515
logger = logging.getLogger(__name__)
@@ -397,14 +397,7 @@ def handle_return(builder, stmt, local_sym_tab, ret_type):
397397
else:
398398
raise ValueError("Failed to evaluate return expression")
399399
elif isinstance(stmt.value, ast.Name):
400-
if stmt.value.id == "XDP_PASS":
401-
builder.ret(ir.Constant(ret_type, 2))
402-
return True
403-
elif stmt.value.id == "XDP_DROP":
404-
builder.ret(ir.Constant(ret_type, 1))
405-
return True
406-
else:
407-
raise ValueError("Failed to evaluate return expression")
400+
return _handle_xdp_return(stmt, builder, ret_type)
408401
else:
409402
raise ValueError("Unsupported return value")
410403

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
11
import logging
2-
import ir
2+
import ast
3+
4+
from llvmlite import ir
35

46
logger: logging.Logger = logging.getLogger(__name__)
57

8+
XDP_ACTIONS = {
9+
"XDP_ABORTED": 0,
10+
"XDP_DROP": 1,
11+
"XDP_PASS": 2,
12+
"XDP_TX": 3,
13+
"XDP_REDIRECT": 4,
14+
}
15+
616

717
def _handle_none_return(builder) -> bool:
818
"""Handle return or return None -> returns 0."""
919
builder.ret(ir.Constant(ir.IntType(64), 0))
1020
logger.debug("Generated default return: 0")
1121
return True
22+
23+
24+
def _handle_xdp_return(stmt: ast.Return, builder, ret_type) -> bool:
25+
"""Handle XDP returns"""
26+
if not isinstance(stmt.value, ast.Name):
27+
return False
28+
29+
action_name = stmt.value.id
30+
31+
if action_name not in XDP_ACTIONS:
32+
raise ValueError(
33+
f"Unknown XDP action: {action_name}. Available: {XDP_ACTIONS.keys()}"
34+
)
35+
36+
value = XDP_ACTIONS[action_name]
37+
builder.ret(ir.Constant(ret_type, value))
38+
logger.debug(f"Generated XDP action return: {action_name} = {value}")
39+
return True

0 commit comments

Comments
 (0)