mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Add _handle_xdp_return
This commit is contained in:
@ -9,7 +9,7 @@ from pythonbpf.type_deducer import ctypes_to_ir
|
|||||||
from pythonbpf.binary_ops import handle_binary_op
|
from pythonbpf.binary_ops import handle_binary_op
|
||||||
from pythonbpf.expr_pass import eval_expr, handle_expr
|
from pythonbpf.expr_pass import eval_expr, handle_expr
|
||||||
|
|
||||||
from .return_utils import _handle_none_return
|
from .return_utils import _handle_none_return, _handle_xdp_return
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -397,14 +397,7 @@ def handle_return(builder, stmt, local_sym_tab, ret_type):
|
|||||||
else:
|
else:
|
||||||
raise ValueError("Failed to evaluate return expression")
|
raise ValueError("Failed to evaluate return expression")
|
||||||
elif isinstance(stmt.value, ast.Name):
|
elif isinstance(stmt.value, ast.Name):
|
||||||
if stmt.value.id == "XDP_PASS":
|
return _handle_xdp_return(stmt, builder, ret_type)
|
||||||
builder.ret(ir.Constant(ret_type, 2))
|
|
||||||
return True
|
|
||||||
elif stmt.value.id == "XDP_DROP":
|
|
||||||
builder.ret(ir.Constant(ret_type, 1))
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
raise ValueError("Failed to evaluate return expression")
|
|
||||||
else:
|
else:
|
||||||
raise ValueError("Unsupported return value")
|
raise ValueError("Unsupported return value")
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,39 @@
|
|||||||
import logging
|
import logging
|
||||||
import ir
|
import ast
|
||||||
|
|
||||||
|
from llvmlite import ir
|
||||||
|
|
||||||
logger: logging.Logger = logging.getLogger(__name__)
|
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:
|
def _handle_none_return(builder) -> bool:
|
||||||
"""Handle return or return None -> returns 0."""
|
"""Handle return or return None -> returns 0."""
|
||||||
builder.ret(ir.Constant(ir.IntType(64), 0))
|
builder.ret(ir.Constant(ir.IntType(64), 0))
|
||||||
logger.debug("Generated default return: 0")
|
logger.debug("Generated default return: 0")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user