mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Compare commits
2 Commits
32int_supp
...
0006e26b08
| Author | SHA1 | Date | |
|---|---|---|---|
| 0006e26b08 | |||
| 5cbd9a531e |
@ -1,7 +1,7 @@
|
|||||||
from .helper_registry import HelperHandlerRegistry
|
from .helper_registry import HelperHandlerRegistry
|
||||||
from .helper_utils import reset_scratch_pool
|
from .helper_utils import reset_scratch_pool
|
||||||
from .bpf_helper_handler import handle_helper_call, emit_probe_read_kernel_str_call
|
from .bpf_helper_handler import handle_helper_call, emit_probe_read_kernel_str_call
|
||||||
from .helpers import ktime, pid, deref, comm, probe_read_str, XDP_DROP, XDP_PASS
|
from .helpers import ktime, pid, deref, comm, probe_read_str, random, XDP_DROP, XDP_PASS
|
||||||
|
|
||||||
|
|
||||||
# Register the helper handler with expr module
|
# Register the helper handler with expr module
|
||||||
@ -65,6 +65,7 @@ __all__ = [
|
|||||||
"deref",
|
"deref",
|
||||||
"comm",
|
"comm",
|
||||||
"probe_read_str",
|
"probe_read_str",
|
||||||
|
"random",
|
||||||
"XDP_DROP",
|
"XDP_DROP",
|
||||||
"XDP_PASS",
|
"XDP_PASS",
|
||||||
]
|
]
|
||||||
|
|||||||
@ -25,6 +25,7 @@ class BPFHelperID(Enum):
|
|||||||
BPF_MAP_DELETE_ELEM = 3
|
BPF_MAP_DELETE_ELEM = 3
|
||||||
BPF_KTIME_GET_NS = 5
|
BPF_KTIME_GET_NS = 5
|
||||||
BPF_PRINTK = 6
|
BPF_PRINTK = 6
|
||||||
|
BPF_GET_PRANDOM_U32 = 7
|
||||||
BPF_GET_CURRENT_PID_TGID = 14
|
BPF_GET_CURRENT_PID_TGID = 14
|
||||||
BPF_GET_CURRENT_COMM = 16
|
BPF_GET_CURRENT_COMM = 16
|
||||||
BPF_PERF_EVENT_OUTPUT = 25
|
BPF_PERF_EVENT_OUTPUT = 25
|
||||||
@ -433,6 +434,28 @@ def bpf_probe_read_kernel_str_emitter(
|
|||||||
return result, ir.IntType(64)
|
return result, ir.IntType(64)
|
||||||
|
|
||||||
|
|
||||||
|
@HelperHandlerRegistry.register("random")
|
||||||
|
def bpf_get_prandom_u32_emitter(
|
||||||
|
call,
|
||||||
|
map_ptr,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
func,
|
||||||
|
local_sym_tab=None,
|
||||||
|
struct_sym_tab=None,
|
||||||
|
map_sym_tab=None,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Emit LLVM IR for bpf_get_prandom_u32 helper function call.
|
||||||
|
"""
|
||||||
|
helper_id = ir.Constant(ir.IntType(64), BPFHelperID.BPF_GET_PRANDOM_U32.value)
|
||||||
|
fn_type = ir.FunctionType(ir.IntType(32), [], var_arg=False)
|
||||||
|
fn_ptr_type = ir.PointerType(fn_type)
|
||||||
|
fn_ptr = builder.inttoptr(helper_id, fn_ptr_type)
|
||||||
|
result = builder.call(fn_ptr, [], tail=False)
|
||||||
|
return result, ir.IntType(32)
|
||||||
|
|
||||||
|
|
||||||
def handle_helper_call(
|
def handle_helper_call(
|
||||||
call,
|
call,
|
||||||
module,
|
module,
|
||||||
|
|||||||
@ -27,6 +27,11 @@ def probe_read_str(dst, src):
|
|||||||
return ctypes.c_int64(0)
|
return ctypes.c_int64(0)
|
||||||
|
|
||||||
|
|
||||||
|
def random():
|
||||||
|
"""get a pseudorandom u32 number"""
|
||||||
|
return ctypes.c_int32(0)
|
||||||
|
|
||||||
|
|
||||||
XDP_ABORTED = ctypes.c_int64(0)
|
XDP_ABORTED = ctypes.c_int64(0)
|
||||||
XDP_DROP = ctypes.c_int64(1)
|
XDP_DROP = ctypes.c_int64(1)
|
||||||
XDP_PASS = ctypes.c_int64(2)
|
XDP_PASS = ctypes.c_int64(2)
|
||||||
|
|||||||
25
tests/passing_tests/helpers/prandom.py
Normal file
25
tests/passing_tests/helpers/prandom.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
from pythonbpf import bpf, bpfglobal, section, BPF, trace_pipe
|
||||||
|
from ctypes import c_void_p, c_int64
|
||||||
|
from pythonbpf.helper import random
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@section("tracepoint/syscalls/sys_enter_clone")
|
||||||
|
def hello_world(ctx: c_void_p) -> c_int64:
|
||||||
|
r = random()
|
||||||
|
print(f"Hello, World!, {r}")
|
||||||
|
return 0 # type: ignore [return-value]
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@bpfglobal
|
||||||
|
def LICENSE() -> str:
|
||||||
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
|
# Compile and load
|
||||||
|
b = BPF()
|
||||||
|
b.load()
|
||||||
|
b.attach_all()
|
||||||
|
|
||||||
|
trace_pipe()
|
||||||
Reference in New Issue
Block a user